import QtQuick 2.15
import QtCharts 2.15

ChartView {
    id: chartView
    title: "动态曲线X"
    antialiasing: true
    animationOptions: ChartView.AllAnimations
    
    property var dataPoints: []
    property real xValue: 0
    property int maxPoints: 10
    
    SplineSeries {
        id: splineSeries
        name: "Spline"
        axisX: valueAxisX
        axisY: valueAxisY
    }
    
    ScatterSeries {
        id: scatterSeries
        name: "Scatter"
        markerSize: 8
        axisX: valueAxisX
        axisY: valueAxisY
    }
    
    ValueAxis {
        id: valueAxisX
        min: 0
        max: 100
    }
    
    ValueAxis {
        id: valueAxisY
        min: 0
        max: 100
    }
    
    Timer {
        id: dataTimer
        interval: 1000
        running: true
        repeat: true
        onTriggered: {
            var newValue = Math.random() * 100
            var x = xValue * 100 / 10
            
            splineSeries.append(x, newValue)
            scatterSeries.append(x, newValue)
            
            // 动态调整X轴范围以实现滚动效果
            if (xValue > 10) {
                valueAxisX.min = (xValue - 10) * 10
                valueAxisX.max = xValue * 10
            }
            
            xValue++
            
            if (splineSeries.count > maxPoints) {
                splineSeries.removePoints(0, 1)
                scatterSeries.removePoints(0, 1)
            }
        }
    }
}

